﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class TownDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static TownDataAccess Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="AddressDataAccess" /> class.
        /// </summary>
        static TownDataAccess() { Instance = new TownDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static Town ExtractRawData(IDataRecord dataReader)
        {
            var town = new Town();


            if (dataReader["CountyId"] != null)
                town.CountyId = (Guid)dataReader["CountyId"];
            if (dataReader["CountryId"] != null)
                town.CountryId = (Guid)dataReader["CountryId"];
            if (dataReader["TownID"] != null)
                town.TownId = (Guid)dataReader["TownID"];
            town.TownName = dataReader["TownName"].ToString();
            return town;


        }
        public Town GetTown(string townId)
        {


            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("townId",townId)
                                      
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetTown, colParameter))
            {
                if (reader.Read())
                {
                    return ExtractRawData(reader);

                }
                return null;
            }
        }
        public int Insert(Town town)
        {
            var colParameter = new Collection<SqlParameter>
                    {
                       new SqlParameter("CountryId",town.CountryId),
                       new SqlParameter("CountryName",town.CountyId),
                       new SqlParameter("TownName",town.TownName),
                       new SqlParameter("TownId",town.TownId)

                    };

            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.InsertTown, colParameter);
            return resuft;
        }


    }
}
